package com.jxb.producer;

import com.jxb.config.QueueDLXConfig;
import com.jxb.utils.MyConstants;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value="/directSend")
    public Object directSend(String message) throws AmqpException, UnsupportedEncodingException {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        messageProperties.setPriority(2);

        //发消息
        rabbitTemplate.send("directqueue",new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : "+message;
    }

    @GetMapping(value="/fanoutSend")
    public Object fanoutSend(String message) throws AmqpException, UnsupportedEncodingException {
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);

        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitTemplate.send(MyConstants.EXCHANGE_FANOUT, "", new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : "+message;
    }

    @GetMapping(value="/topicSendHunanIT")
    public Object topicSend(String routingKey,String message) throws AmqpException, UnsupportedEncodingException {
        if(null == routingKey) {
            routingKey="hebei.IT";
        }
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitTemplate.send("topicExchange", routingKey, new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : routingKey >"+routingKey+";message > "+message;
    }

    @GetMapping(value="/headerSend")
    public Object headerSend(String txTyp,String busTyp,String message) throws AmqpException, UnsupportedEncodingException {
        if(null == txTyp) {
            txTyp="0";
        }
        if(null == busTyp) {
            busTyp="0";
        }
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        messageProperties.setHeader("txTyp", txTyp);
        messageProperties.setHeader("busTyp", busTyp);
        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitTemplate.send("headerExchange", "uselessRoutingKey", new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : txTyp >"+txTyp+";busTyp > "+busTyp;
    }

    @GetMapping(value="/directQuorum")
    public Object directQuorum(String message) throws AmqpException, UnsupportedEncodingException {
        //设置部分请求参数
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        messageProperties.setPriority(2);
        //设置消息转换器，如json
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //将对象转换成json再发送。
//		rabbitTemplate.convertandsend("",Object);
        //发消息
        rabbitTemplate.send(MyConstants.QUEUE_QUORUM,new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : "+message;
    }

    //	@ApiOperation(value="stream队列发送接口",notes="直接发送到队列。stream队列")
//	@GetMapping(value="/directStream")
    public Object directStream(String message) throws AmqpException, UnsupportedEncodingException {
        //设置部分请求参数
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        messageProperties.setPriority(2);
        //设置消息转换器，如json
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //将对象转换成json再发送。
//		rabbitTemplate.convertandsend("",Object);
        //发消息
        rabbitTemplate.send(MyConstants.QUEUE_STREAM,new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : "+message;
    }

    @GetMapping(value="/sendTTLDLX")
    public Object sendTTLDLX() {

        System.out.println(new Date());
        String str = "{'id' :'2'}";
        //发送消息给死信队列
        rabbitTemplate.convertAndSend(QueueDLXConfig.QUEUEDLX_NAME, str.getBytes(), new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //存活时间 毫秒  2分钟
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        });
        return "发送成功";
    }
}
